//
//  XClockInTableViewCell.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XClockInTableViewCell.h"
#import "XClockInModel.h"


@interface XClockInTableViewCell (){
    
    UILabel* _nameLb;//名称
    UILabel* _countLb;//累计
    UILabel* _timeLb;//开始时间
    UIButton* _clockBtn;
}
/// 显示内容视图
@property(nonatomic,weak) MyLinearLayout* contentLayout;

@end

@implementation XClockInTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = PX(5);
        
        [self initSubViews];
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 10;
    frame.size.width -= 2 * 10;
    [super setFrame:frame];
}

- (void)initSubViews{
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    contentLayout.cacheEstimatedRect = YES;
    contentLayout.padding = UIEdgeInsetsMake(16, 16, 16, 16);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    

    MyLinearLayout *descLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    descLayout.myHeight = MyLayoutSize.wrap;
    descLayout.myMargin = 0;
    descLayout.weight = 1.0;
    [contentLayout addSubview:descLayout];
    
    
    UILabel* nameLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_BLACK_DARK text:@"是多少都是就看电视的"];
    nameLb.myMargin = 0;
    [descLayout addSubview:nameLb];
    _nameLb = nameLb;
    
    UILabel* timeLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"开始时间:xxxx.xx.xx"];
    timeLb.myHorzMargin = 0;
    timeLb.myTop = 12;
    [descLayout addSubview:timeLb];
    _timeLb = timeLb;
    
    UILabel* countLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"累计：0"];
    countLb.myHorzMargin = 0;
    countLb.myTop = 20;
    [descLayout addSubview:countLb];
    _countLb = countLb;
    
    
    UIButton* clockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [clockBtn setImage:[UIImage imageNamed:@"a_select_n"] forState:UIControlStateNormal];
    [clockBtn setImage:[UIImage imageNamed:@"a_select_h"] forState:UIControlStateSelected];
    [clockBtn addTarget:self action:@selector(clockClicked:) forControlEvents:UIControlEventTouchUpInside];
    clockBtn.mySize = CGSizeMake(PX(40), PX(40));
    clockBtn.myCenterX = 0;
    clockBtn.myMargin = 0;
    [self.contentLayout addSubview:clockBtn];
    
}

- (void)setModel:(XClockInModel *)model{
    _model = model;
    
    _nameLb.text = model.name;
    [_nameLb sizeToFit];
    
    
    _timeLb.text = [NSString stringWithFormat:@"开始时间：%@",[model.beginDate stringWithFormat:@"yyyy.MM.dd"]];
    [_timeLb sizeToFit];
    
    _countLb.text = [NSString stringWithFormat:@"累计：%ld",model.records.count];
    [_countLb sizeToFit];
    
    _clockBtn.selected = model.todayState;
}

///打卡
- (void)clockClicked:(UIButton*)sender{
    sender.selected = !sender.selected;
    
    if(sender.selected){
        XClockInDate* date = [XClockInDate new];
        date.date = [NSDate date];
        date.clockId = self.model.id;
        [[RLMRealm defaultRealm] beginWriteTransaction];
        [self.model.records addObject:date];
        [[RLMRealm defaultRealm] addOrUpdateObject:self.model];
        [[RLMRealm defaultRealm] commitWriteTransaction];
    }else{
        int index = -1;
        for (int i = 0; i < self.model.records.count; i++) {
            XClockInDate* date = self.model.records[i];
            NSString* dateStr = [date.date stringYearMonthDay];
            NSString* nowStr = [[NSDate date] stringYearMonthDay];
            if([dateStr isEqualToString:nowStr]){
                index = i;
                break;
            }
        }
        if(index >= 0){
            [[RLMRealm defaultRealm] beginWriteTransaction];
            [self.model.records removeObjectAtIndex:index];
            [[RLMRealm defaultRealm] addOrUpdateObject:self.model];
            [[RLMRealm defaultRealm] commitWriteTransaction];
        }
    }
    
    _countLb.text = [NSString stringWithFormat:@"累计：%ld",self.model.records.count];
    [_countLb sizeToFit];
}

#pragma private -- Size

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
    
    CGSize cellSize = CGSizeZero;
    if (@available(iOS 11.0, *)) {
        cellSize = [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width - self.safeAreaInsets.left - self.safeAreaInsets.right - 2*10, targetSize.height)];
    } else {
        CGSize size = targetSize;
        size.width = size.width - 2*10;
        cellSize = [self.contentLayout sizeThatFits:size];  //如果使用系统自带的分割线，请记得将返回的高度height+1
    }
    return cellSize;
}
@end
